var LoadingAlram = {
    initialize : function(){

    	if($('#loginCheck').data("loginyn")){


            $.ajax({ url: "/mind/checkReplyAndContents.do", success: function(data){

                if(data["replyCnt"] != 0 ){
                    $('.bubble').remove();

                    $('#Messages').append("<div class='bubble'><p class='label' id='lbl_cmt_cnt' style='cursor: pointer;' > " + data["replyCnt"] + "</p></div>");
                }

                //Update your dashboard gauge
                if(data["replyData"].length > 0){
                    var audio = $('audio')[0];
                    audio.play();
                }

                if(data["replyDataContents"].length > 0){
                    var rowCnt = data["replyDataContents"].length;
                    var tag = "";

                    for(var i = 0; i < rowCnt ; i++){
                        tag += "<div style='height: 21px;width: 100px;display: inline-block;text-overflow: ellipsis;overflow: hidden;white-space:nowrap;' >";
                        tag += data["replyDataContents"][i]["REPLY_USER_NAME"];
                        tag += "</div>";
                        tag += "<div style='height: 21px;width: 150px;display: inline-block;text-overflow: ellipsis;overflow: hidden;white-space:nowrap;'>";
                        tag += data["replyDataContents"][i]["REPLY_CONTENT"];
                        tag += "</div>";
                        tag += "<a href='/views/blogpost.do?BLOG_NO="+ data["replyDataContents"][i]["PARENT_BLOG_NO"] + "' class='ketchup tooltip' title='This is my links tooltip message!'>Link</a>";
                        tag += "</br>";
                    }

                    $('.bubble').tooltipster({
                        content: $(tag),
                        contentAsHTML : true,
                        autoClose : true,
                        interactive : true,
                        position : 'bottom'
                    });
                }

            }, dataType: "json"});


    		 (function poll(){
                 COMMAJAX["setTimeOut"] = setTimeout(function(){
                     $.ajax({ url: "/mind/checkReplyAndContents.do", success: function(data){

                         if(data["replyCnt"] != 0 ){
                             $('.bubble').remove();

                             $('#Messages').append("<div class='bubble'><p class='label' id='lbl_cmt_cnt' style='cursor: pointer;' > " + data["replyCnt"] + "</p></div>");
                         }

                         //Update your dashboard gauge
                         if(data["replyData"].length > 0){
                             var audio = $('audio')[0];
                             audio.play();
                         }

                         if(data["replyDataContents"].length > 0){
                             var rowCnt = data["replyDataContents"].length;
                             var tag = "";

                             for(var i = 0; i < rowCnt ; i++){
                                 tag += "<div style='height: 21px;width: 100px;display: inline-block;text-overflow: ellipsis;overflow: hidden;white-space:nowrap;' >";
                                 tag += data["replyDataContents"][i]["REPLY_USER_NAME"];
                                 tag += "</div>";
                                 tag += "<div style='height: 21px;width: 150px;display: inline-block;text-overflow: ellipsis;overflow: hidden;white-space:nowrap;'>";
                                 tag += data["replyDataContents"][i]["REPLY_CONTENT"];
                                 tag += "</div>";
                                 tag += "<a href='/views/blogpost.do?BLOG_NO="+ data["replyDataContents"][i]["PARENT_BLOG_NO"] + "' class='ketchup tooltip' title='This is my links tooltip message!'>Link</a>";
                                 tag += "</br>";
                             }

                             $('.bubble').tooltipster({
                                 content: $(tag),
                                 contentAsHTML : true,
                                 autoClose : true,
                                 interactive : true,
                                 position : 'bottom'
                             });
                         }



                         //Setup the next poll recursively
                         poll();
                     }, dataType: "json"});
                 }, 10000);
             })();
    	}
    	
    	$('#logout_btn').unbind('click').bind('click', function(){
            COMMAJAX.common_ajax({
                url : "/mind/home/logoutcheck.do",
                callback_fnc : function(){

                    clearInterval(COMMAJAX.setTimeOut);

                    window.setTimeout('location.reload()', 1000);
                }
            });
        });
    	
    	$('#current').on("click" ,function(){
            var paramData = {};
            paramData["USER_PWD"] = $('#txt_pwd').val();
            paramData["USER_ID"] = $('#txt_email').val();
            COMMAJAX.common_ajax({
                url : "/mind/home/loginCheck.do"
                ,data : paramData
                ,callback_fnc :  function(data){
                    if(data.rows == "N"){
                        alert("로그인 아이디 또는 비밀번호가 올바르지 않습니다. 다시확인해주세!")
                    }else{
                        $('#txt_email').hide();
                        $('#txt_pwd').hide();
                        $('#current').remove();
                        $('#joinLink').remove();
                        if(data.rows.AUTH_YN){
                            if(data.rows.AUTH_YN == "Y"){
                                $('header ul:eq(1)').append('<li><a href="/views/work.do" >관리페이지</a></li>');
                            }
                        }
                        $('header ul:eq(1)').append('<li><a class="logout_btn" id="logout_btn">나가기</a></li>');
                        $('#header_left li:eq(0)').html("<strong>" + data.rows.USER_NAME +"</strong> 님, 반갑습니다.")

                        if(data.rows.CNT != 0){
                            var tag = "";
                            tag += '<li><div id="Messages">';
                            tag += '<div class="bubble">';
                            tag += '<p class="label" id="lbl_cmt_cnt" style="cursor: pointer;">' + data.rows.CNT +'</p>';
                            tag += '</div>';
                            tag += '</div></li>';

                            $('#header_right').append(tag);
                        }

                        if(data.rows["content"].length > 0){
                            var audio = $('audio')[0];
                            audio.play();
                        }

                        if(data.rows["contentMap"].length > 0){
                            var rowCnt = data.rows["contentMap"].length;
                            var tag = "";

                            for(var i = 0; i < rowCnt ; i++){
                                tag += "<div style='height: 21px;width: 100px;display: inline-block;text-overflow: ellipsis;overflow: hidden;white-space:nowrap;' >";
                                tag += data.rows["contentMap"][i]["REPLY_USER_NAME"];
                                tag += "</div>";
                                tag += "<div style='height: 21px;width: 150px;display: inline-block;text-overflow: ellipsis;overflow: hidden;white-space:nowrap;'>";
                                tag += data.rows["contentMap"][i]["REPLY_CONTENT"];
                                tag += "</div>";
                                tag += "<a href='/views/blogpost.do?BLOG_NO="+ data.rows["contentMap"][i]["PARENT_BLOG_NO"] + "' class='ketchup tooltip' title='This is my links tooltip message!'>Link</a>";
                                tag += "</br>";
                            }

                            $('.bubble').tooltipster({
                                content: $(tag),
                                contentAsHTML : true,
                                autoClose : true,
                                interactive : true,
                                position : 'bottom'
                            });
                        }

                        (function poll(){
                            COMMAJAX["setTimeOut"] = setTimeout(function(){
                                $.ajax({ url: "/mind/checkReplyAndContents.do", success: function(data){

                                    if(data["replyCnt"] != 0 ){
                                        $('.bubble').remove();

                                        $('#Messages').append("<div class='bubble'><p class='label' id='lbl_cmt_cnt' style='cursor: pointer;' > " + data["replyCnt"] + "</p></div>");
                                    }

                                    //Update your dashboard gauge
                                    if(data["replyData"].length > 0){
                                        var audio = $('audio')[0];
                                        audio.play();

                                    }

                                    if(data["replyDataContents"].length > 0){
                                        var rowCnt = data["replyDataContents"].length;
                                        var tag = "";

                                        for(var i = 0; i < rowCnt ; i++){
                                            tag += "<div style='height: 21px;width: 100px;display: inline-block;text-overflow: ellipsis;overflow: hidden;white-space:nowrap;' >";
                                            tag += data["replyDataContents"][i]["REPLY_USER_NAME"];
                                            tag += "</div>";
                                            tag += "<div style='height: 21px;width: 150px;display: inline-block;text-overflow: ellipsis;overflow: hidden;white-space:nowrap;'>";
                                            tag += data["replyDataContents"][i]["REPLY_CONTENT"];
                                            tag += "</div>";
                                            tag += "<a href='/views/blogpost.do?BLOG_NO="+ data["replyDataContents"][i]["PARENT_BLOG_NO"] + "' class='ketchup tooltip' title='This is my links tooltip message!'>Link</a>";
                                            tag += "</br>";
                                        }

                                        $('.bubble').tooltipster({
                                            content: $(tag),
                                            contentAsHTML : true,
                                            autoClose : true,
                                            interactive : true,
                                            position : 'bottom'
                                        });
                                    }

                                    //Setup the next poll recursively
                                    poll();
                                }, dataType: "json"});
                            }, 10000);
                        })();

                        //$('.bubble').append("<div id='tooltip_call' style='height: 5px;width: 5px' ></div>");
                        if($('#contact_form ul li').length != 0){
                            $('#contact_form ul li').remove();
                        }

                        $('#logout_btn').unbind('click').bind('click', function(){
                            COMMAJAX.common_ajax({
                                url : "/mind/home/logoutcheck.do",
                                callback_fnc : function(){

                                    clearInterval(COMMAJAX.setTimeOut);

                                    window.setTimeout('location.reload()', 1000);
                                }
                            });
                        });
                    }
                }
            });
        });
    }
}


!function($){
    LoadingAlram.initialize();
}(jQuery);